package tec.stan.ling.core.util;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;

public class WorkspaceUtil {

	public static final String ENCODING = "UTF-8";

	public static final String PROJECT = ".ling";//$NON-NLS-1$

	public static final String TOPO_Folder = "topo";//$NON-NLS-1$

	public static IFolder getTopoFolder(IProgressMonitor monitor) throws Exception {
		IProject project = getProject(monitor);
		IFolder folder = project.getFolder(TOPO_Folder);
		if (!folder.exists()) {
			folder.create(true, true, monitor);
		}
		return folder;
	}

	public static IProject getProject(IProgressMonitor monitor)
			throws Exception {
		IWorkspace ws = ResourcesPlugin.getWorkspace();
		IWorkspaceRoot wsRoot = ws.getRoot();
		IProject project = wsRoot.getProject(PROJECT);
		if (!project.exists()) {
			IProjectDescription desc = ws.newProjectDescription(project
					.getName());
			project.create(desc, monitor);
			project.open(IResource.BACKGROUND_REFRESH, monitor);
			project.setDefaultCharset(ENCODING, monitor);
		}
		return project;
	}
}
